package com.example.showlistexample;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_user, et_password;
    private Button login, registe;
    private List<Person> list = new ArrayList<>();
    private Toolbar toolbar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        if(DbUtils.getLiteOrm() == null){
            DbUtils.createDb(this, "ShowList");
        }
        toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("登陆");
        }
        setSupportActionBar(toolbar);

        login = (Button) findViewById(R.id.denglu);
        registe = (Button) findViewById(R.id.zhuce);
        et_user = (EditText) findViewById(R.id.zhanghao);
        et_password = (EditText) findViewById(R.id.mima);
        login.setOnClickListener(this);
        registe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.zhuce:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.denglu:
                String user = et_user.getText().toString();
                String password = et_password.getText().toString();
                if (user.isEmpty() || password.isEmpty()){
                    Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                list = DbUtils.getQueryByWhere(Person.class, "name", "password", new String[]{user}, new String[]{password});
                if (list.size() == 0) {
                    Toast.makeText(this, "用户名不存在或密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                }

                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
                finish();

                break;
            default:
                break;
        }
    }
}
